<?php
// +----------------------------------------------------------------------
// | TYCMS [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: aliang <461321026@qq.com>
// +----------------------------------------------------------------------
namespace app\home\controller;

use think\Db;

class Shop extends Base
{
    var $menu_id = 2;
    protected $groupid = 0;
    protected function _initialize()
    {
        parent::_initialize();
        $hid = session('hid');
        $this->groupid = 0;
        
        if(!empty($hid)){
            $member  = session('user') ? session('user'):Db::name('member_list')->get($hid);
            $this->groupid = isset($member['member_list_groupid']) ? $member['member_list_groupid']:0;
        }
        $this->assign('groupid',$this->groupid);
        
        
        $mytitle = '我的国珍直销商城-'.config('options.site_name');
        $code = input('procode','','trim');
        $code = htmlspecialchars(strip_tags($code));
        if(!empty($code)){
           $members = Db::name("member_list")->where("code",$code)->find();
           if(!empty($members) && $members['member_list_groupid'] > 1){
              cookie('procode',$code,7*24*3600);
              session('procode',$code);
              if(!empty($member)){
                 $mytitle = $member['member_list_nickname'].'的国珍直销商城-'.config('options.site_name');
              }else{
                 $mytitle = $members['member_list_nickname'].'的国珍直销商城-'.config('options.site_name');
              }
           }else{
              $this->redirect(url('home/shop/index'));
           } 
        }
        $this->assign('mytitle',$mytitle);
        
    }
    
    //国珍商城
    public function index()
    {
        $cid = input("cid",0,"intval");
        $parentid = 0;
        $menu=Db::name('menu')->find($this->menu_id);
		if(empty($menu)){
		    $this->error(lang('operation not valid'));
		}
		$pid   = input('pid',0);
		$level = input('level',0);
		$tplname = $menu['menu_newstpl'];
    	$tplname = $tplname ? $tplname:'news';
    	$this->assign('menu',$menu);
    	$where = ["g.status"=>1];
    	//分类
    	$cate_list = Db::name('category')->where(["model"=>"goods"])->select();
    	if($cid>0){
        	foreach($cate_list as $c){
        	    if($c["id"] == $cid){
        	        $parentid = $c["parentid"];
        	        break;
        	    }
        	}
        	$where["gc.cid"]=$cid;
    	}
    	
    	$goods_list = Db::name('goods')->alias("g")->join("__GOODS_CATE__ gc","gc.gid = g.id","LEFT")
    	               ->join("__GOODS_PRICE__ gp","gp.gid = g.id","LEFT")
                	   ->where($where)->field("g.*,gc.cid,gp.price_1,gp.price_2,gp.price_3")->paginate(12);
    	
    	$page       = $goods_list->render();
    	$goods_list = $goods_list->All();
    	foreach($goods_list as $k=>$v){
    	    foreach($v as $t=>$g){
    	      if(strpos($t,'price') !==false)
    	         $goods_list[$k][$t] = str_replace('.00','',$g); 
    	    }
    	}
    	$this->assign('pager',$page);
        $this->assign("goods_list",$goods_list);
    	//$category  = menu_left($cate_list,'id','parentid','─',$pid,$level,$level*20);
    	$tree = new \Tree($cate_list);
    	$category = $tree->get_arraylist($cate_list);
    	if(request()->isMobile()){
    	    foreach($category as $k=>$v){
    	        $category[$k]['cate_name'] = $v['cate_name'];
    	    }
    	}
    	$this->assign('category',$category);
    	$this->assign('parentid',$parentid);
    	$this->assign('cid',$cid);
		return $this->view->fetch(":$tplname");
    }
    
    
    //列表页
    public function mlist(){
        $menu=Db::name('menu')->find($this->menu_id);
        if(empty($menu)){
            $this->error(lang('operation not valid'));
        }
        $tplname = $menu['menu_newstpl'];
        $tplname = $tplname ? $tplname:'news';
        
        dump($tplname);exit;
    }
    
    
    //详细页
    public function show(){
        $good_id = input("id",0,"intval");
        if($good_id<=0){
            $this->error(lang('operation not valid'));
        }
        $goods = Db::name('goods')->alias("g")->join("__GOODS_CATE__ gc","gc.gid = g.id","LEFT")
                    ->join("__CATEGORY__ c","gc.cid = c.id","LEFT") 
                    ->join("__GOODS_PRICE__ gp","gp.gid = g.id","LEFT")
                    ->where(["g.id"=>$good_id])->field("g.*,c.cate_name,gc.cid,gp.price_1,gp.price_2,gp.price_3")->find();
        
        $uid        = session('hid') ? session('hid'):0;
        $member_group_id = 0;
        if($uid >0){
            $member  = session('user') ? session('user'):'';
            $member_group_id = isset($member['member_list_groupid']) ? $member['member_list_groupid']:0;
        }
        if($member_group_id == 0)
            $goods['true_price'] = $goods['price'];
        else{
            $keys = 'price_'.$member_group_id;
            $goods['true_price'] = isset($goods[$keys]) ? $goods[$keys]:$goods['price'];
        }
        
        
        
        
        $goods['img_list'] = [];
        $img_arr = explode(',',empty($goods['goods_pic_allurl']) ? $goods['thumb']:$goods['goods_pic_allurl']);
        foreach($img_arr as $v){
            if(!empty($v))
                $goods['img_list'][] = $v;
        }
        
         
        $this->assign("goods",$goods);
        return $this->view->fetch();
    }
     
}
